{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a43a8d5a",
   "metadata": {},
   "source": [
    "# Take a Tour: Responsible AI Toolbox"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "88a1b04b",
   "metadata": {},
   "source": [
    "The Responsible AI Toolbox is a customizable, interoperable tool where you can select components that perform analytical functions relevant to one of these areas: \n",
    "\n",
    "\n",
    "\n",
    "* Model Assessment and Debugging, which involves determining how and why AI systems behave the way they do, identifying and diagnosing issues, then using that knowledge to take targeted steps to improve their performance. This can be encapsulated in a multi-step workflow:\n",
    "\n",
    "![Multi-step workflow](img/tour-1.png)\n",
    "\n",
    "* Decision-making: While machine learning models are particularly powerful in identifying patterns in data and making predictions, they offer little support to boost decision making processes, which involves estimating how a real-world outcome changes in the presence of an intervention, or “interrogating” a model to determine what changes to a particular datapoint would flip the model decision to become different/favorable.\n",
    "\n",
    "\n",
    "\n",
    "Each component has a variety of tabs and buttons. Take this tour to familiarize yourself with the different components of the dashboard and the options and functionalities available in each."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "699793a0",
   "metadata": {},
   "source": [
    "## Table of Contents\n",
    "* [Error Analysis](#Error-Analysis)\n",
    "* [Data Explorer](#Data-Explorer)\n",
    "* [Model Statistics](#Model-Statistics)\n",
    "* [Interpretability](#Interpretability)\n",
    "* [Counterfactuals](#Counterfactuals)\n",
    "* [Causal Inferencing](#Causal-Inferencing)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "337e4515",
   "metadata": {},
   "source": [
    "## Error Analysis"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1faeefac",
   "metadata": {},
   "source": [
    "### Cohorts"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2390554",
   "metadata": {},
   "source": [
    "At the top of the dashboard, you can create cohorts -- subgroups of datapoints sharing specified characteristics -- to focus your analysis on."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9567d74",
   "metadata": {},
   "source": [
    "![Top section of Responsible AI Toolbox showing global cohorts](img/tour-2.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c8a3c49",
   "metadata": {},
   "source": [
    "1. **Cohort settings:** allows you to view and modify the details of each cohort in a side panel\n",
    "2. **Dashboard configuration:** allows you to view and modify the layout of the overall dashboard in a side panel\n",
    "3. **Switch global cohort:** allows you to select a different cohort and view its statistics in a popup\n",
    "4. **Create new cohort:** allows you to add a new cohort"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59e034f1",
   "metadata": {},
   "source": [
    "Clicking the \"Cohort settings\" button reveals a side panel with details on all existing cohorts."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a481f86",
   "metadata": {},
   "source": [
    "![Cohort settings side panel](img/tour-3.png)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c01a3bb8",
   "metadata": {},
   "source": [
    "1. **Switch global cohort:** allows you to select a different cohort and view its statistics in a popup\n",
    "2. **Create new cohort:** allows you to add a new cohort\n",
    "3. **Cohort list:** contains the number of data points, the number of filters, the percent of error coverage, and the error rate for each cohort"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3482d80",
   "metadata": {},
   "source": [
    "Clicking the \"Dashboard settings\" button reveals a side panel with details on the dashboard layout."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf593f9b",
   "metadata": {},
   "source": [
    "<div>\n",
    "<img src=\"./img/tour-4.png\" width=\"300\"/>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b5dc608",
   "metadata": {},
   "source": [
    "1. **Dashboard Component:** lists the name of the component\n",
    "2. **Delete:** removes the component from the dashboard\n",
    "*Note: Each component row can be clicked and dragged to move it to a different location*"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37e82ef5",
   "metadata": {},
   "source": [
    "Clicking the \"Switch global cohort\" button on the dashboard or in the \"Cohort settings\" sidebar creates a popup that allows you to do that."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8270a9a3",
   "metadata": {},
   "source": [
    "![Popup to change the cohort](img/tour-5.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c81fd32",
   "metadata": {},
   "source": [
    "Clicking the \"Create new cohort\" button on the top of the Toolbox or in the \"Cohort settings\" sidebar creates a sidebar that allows you to do that."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "328c0016",
   "metadata": {},
   "source": [
    "![Cohort creation sidebar options](img/tour-6.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b24d8be4",
   "metadata": {},
   "source": [
    "1. **Index:** filters by the position of the datapoint in the full dataset\n",
    "2. **Dataset:** filters by the value of a particular feature in the dataset\n",
    "3. **Predicted Y:** filters by the prediction made by the model\n",
    "4. **True Y:** filters by the actual value of the target feature\n",
    "5. **Classification Outcome:** for classification problems, filters by type and accuracy of classification\n",
    "6. **Numerical Values:** filter by a Boolean operation over the values (i.e. select datapoints where age < 64)\n",
    "7. **Categorical Values:** filter by a list of values that should be included"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a42780b7",
   "metadata": {},
   "source": [
    "### Tree View"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c5e3177",
   "metadata": {},
   "source": [
    "The first tab of the Error Analysis component is the tree view, which illustrates how model failure is distributed across different cohorts."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f281456",
   "metadata": {},
   "source": [
    "![errortree](img/tour-7.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4d74fa7",
   "metadata": {},
   "source": [
    "1. **Heatmap view:** switches to heatmap visualization of error distribution\n",
    "2. **Feature list:** allows you to modify the features used in the heatmap using a side panel\n",
    "3. **Error coverage:** displays the percentage of all error in the dataset concentrated in the selected node\n",
    "4. **Error rate:** displays the percentage of failures of all the datapoints in the selected node\n",
    "5. **Node:** represents a cohort of the dataset, potentially with filters applied, and the number of errors out of the total number of datapoints in the cohort\n",
    "6. **Fill line:** visualizes the distribution of datapoints into child cohorts based on filters, with number of datapoints represented through line thickness\n",
    "7. **Selection information:** contains information about the selected node in a side panel\n",
    "8. **Save as a new cohort:** creates a new cohort with the given filters\n",
    "9. **Instances in the base cohort:** displays the total number of points in the entire dataset, as well as the number of correctly and incorrectly predicted points\n",
    "10. **Instances in the selected cohort:** displays the total number of points in the selected node, as well as the number of correctly and incorrectly predicted points\n",
    "11. **Prediction path (filters):** lists the filters placed over the full dataset to create this smaller cohort"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e53ec726",
   "metadata": {},
   "source": [
    "Clicking on the \"Feature list\" button displays a side panel."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "704a11aa",
   "metadata": {},
   "source": [
    "![errortreeconfiguration](img/tour-8.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07a96c79",
   "metadata": {},
   "source": [
    "1. **Search features:** allows you to find specific features in the dataset\n",
    "2. **Features:** lists the name of the feature in the dataset\n",
    "3. **Importances:** visualizes the relative global importances of each feature in the dataset\n",
    "4. **Check mark:** allows you to add or remove the feature from the tree map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "678d0d90",
   "metadata": {},
   "source": [
    "### Heatmap View"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f430c5da",
   "metadata": {},
   "source": [
    "Clicking on the \"Heatmap view\" tab switches to a different view of the error in the dataset. You can click on one or many heatmap cells and create new cohorts."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e129e29c",
   "metadata": {},
   "source": [
    "\n",
    "<div>\n",
    "<img src=\"img/tour-9.png\" width=\"500\"/>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bcde565a",
   "metadata": {},
   "source": [
    "1. **No. Cells:** displays the number of cells selected\n",
    "2. **Error coverage:** displays the percentage of all errors concentrated in the selected cell(s)\n",
    "3. **Error rate:** displays the percentage of failures of all datapoints in the selected cell(s)\n",
    "4. **Axis features:** selects the intersection of features to display in the heatmap\n",
    "5. **Cells:** represents a cohort of the dataset, with filters applied, and the percentage of errors out of the total number of datapoints in the cohort. A blue outline indicates selected cells, and the darkness of red represents the concentration of failures\n",
    "6. **Prediction path (filters):** lists the filters placed over the full dataset for each selected cohort"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21f87fad",
   "metadata": {},
   "source": [
    "## Data Explorer"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28e9fab8",
   "metadata": {},
   "source": [
    "![dataexplorer1](img/tour-10.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9975358",
   "metadata": {},
   "source": [
    "1. **X-axis:** displays the type of value being plotted horizontally, modify by clicking to display a side panel\n",
    "2. **Y-axis:** displays the type of value being plotted vertically, modify by clicking to display a side panel\n",
    "3. **Chart type:** specifies whether the plot is aggregating values across all datapoints\n",
    "4. **Aggregate plot:** displays data in bins or categories along the x-axis"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7fd5025",
   "metadata": {},
   "source": [
    "Clicking on the \"Individual datapoints\" option under \"Chart type\" shifts to a disaggregated view of the data."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83cb2307",
   "metadata": {},
   "source": [
    "![dataexplorer2](img/tour-11.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c627bae8",
   "metadata": {},
   "source": [
    "1. **Color value:** allows you to select the type of legend used to group datapoints\n",
    "2. **Disaggregate plot:** scatterplot of datapoints along specified axes"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "562614a3",
   "metadata": {},
   "source": [
    "Clicking on the labels of the axes displays a popup."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "596f81e9",
   "metadata": {},
   "source": [
    "![image-2.png](img/tour-12.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2413d2ae",
   "metadata": {},
   "source": [
    "1. **Select your axis alue:** allows you to select the value displayed on the axis, with the same options and variety as cohort creation\n",
    "2. **Should dither:** adds optional noise to the data to avoid overlapping points in the scatterplot"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05d28245",
   "metadata": {},
   "source": [
    "## Model Statistics"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a923864",
   "metadata": {},
   "source": [
    "![model-stats](img/tour-13.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "211d409e",
   "metadata": {},
   "source": [
    "1. **X-axis:** allows you to select a particular statistic of model prediction\n",
    "2. **Y-axis:** plots model statistics for a specified cohort of data\n",
    "3. **Metrics:** lists the accuracy, precision, recall, false positive rate, and false negative rate of the model"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df764020",
   "metadata": {},
   "source": [
    "Clicking the x-axis label allows you to select which cohorts to plot statistics for."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2d61e2e",
   "metadata": {},
   "source": [
    "![axis-setup](img/tour-14.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00d76e2d",
   "metadata": {},
   "source": [
    "1. **Cohort:** allows you to view model statistics of premade cohorts\n",
    "2. **Dataset:** allows you to view model statistics of a feature across bins or categories "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cd4541e",
   "metadata": {},
   "source": [
    "Clicking the y-axis label allows you to select the metric(s) to plot for the cohorts."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01678d8e",
   "metadata": {},
   "source": [
    "![axis-setup-options](img/tour-15.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79dcbf4b",
   "metadata": {},
   "source": [
    "1. **Predicted Y:** plots based on the prediction made by the model\n",
    "2. **True Y:** plots based on the actual value of the target feature\n",
    "3. **Classification outcome:** for classification problems, plots based on the type and accuracy of classification\n",
    "4. **Prediction probabilities:** plots based on the probability of a certain prediction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39c81657",
   "metadata": {},
   "source": [
    "## Interpretability"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a64c63d",
   "metadata": {},
   "source": [
    "### Global Explanations"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ea0071c",
   "metadata": {},
   "source": [
    "![global-explanation](img/tour-16.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c243a2b4",
   "metadata": {},
   "source": [
    "1. **Top features:** lists the most important global features for a prediction and allows you to change it through buttons or a slider\n",
    "2. **Aggregate feature importance:** visualizes the weight of each feature in influencing model decisions across all predictions\n",
    "3. **Dataset cohorts:** displays the legend for aggregate feature importances for each cohort of data\n",
    "4. **Sort by:** allows you to select which cohort's importances to sort the aggregate feature importance graph by\n",
    "5. **Chart type:** allows you to select between a bar plot view of average importances for each feature and a box plot of importances for all data\n",
    "6. **Feature importance of ____: Class: ____:** plots the importance of a particular feature across the predictions in the cohort\n",
    "7. **View dependence plot for:** selects the feature whose importances you want to plot\n",
    "8. **View cohort:** selects the cohort whose importances you want to plot"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79d6a0d5",
   "metadata": {},
   "source": [
    "Clicking the \"Individual feature importances\" tab shifts views to explain how features influence the predictions made on specific datapoints."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31aeaa5f",
   "metadata": {},
   "source": [
    "### Local Explanations"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe78d4f1",
   "metadata": {},
   "source": [
    "![local-explanation-table](img/tour-17.png)\n",
    "![global-explanation-chart](img/tour-18.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36da7473",
   "metadata": {},
   "source": [
    "1. **Filter by:** selects the cohort of points to display\n",
    "2. **Scatter plot:** switches to and from the scatter plot view of the points\n",
    "3. **Point selection:** selects the points to display in the feature importance plot or the ICE plot\n",
    "4. **Feature importance plot:** bar plot of the importance of each feature for the model's prediction on the selected datapoint(s)\n",
    "5. **Individual conditional expectation (ICE) plot:** switches to the ICE plot showing model predictions across a range of values of a particular feature\n",
    "6. **Sort by:** allows you to select the point (of those checked above) whose feature importances are displayed in descending order on the feature importance plot\n",
    "7. **Top 1-n features:** allows you to specify the number of features to show importances for through a slider\n",
    "8. **Bar plot:** displays the importance of each feature in the dataset for the model prediction of the selected datapoints"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27e9a144",
   "metadata": {},
   "source": [
    "Clicking \"Individual conditional expectation (ICE) plot\" switches views to show how model prediction for the selected datapoint varies across values of a given feature."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe7e7fb2",
   "metadata": {},
   "source": [
    "![ICE-plot](img/tour-19.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc9bc5be",
   "metadata": {},
   "source": [
    "1. **Min:** specifies the lower bound of the range of predictions in the ICE plot\n",
    "2. **Max:** specifies the upper bound of the range of predictions in the ICE plot\n",
    "3. **Steps:** specifies the number of points to show predictions for within the interval\n",
    "4. **Feature:** specifies the feature to make predictions for"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed243068",
   "metadata": {},
   "source": [
    "## Counterfactuals"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f209261",
   "metadata": {},
   "source": [
    "![what-if](img/tour-20.png)\n",
    "![what-if-1](img/tour-21.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a89cf633",
   "metadata": {},
   "source": [
    "1. **Filter by:** selects the cohort of points to display\n",
    "2. **Scatter plot:** switches to and from the scatter plot view of the points\n",
    "3. **Point selection:** selects the point to create a counterfactual for and display in the top ranking features plot\n",
    "4. **Selected datapoint:** performs the same action as the point selection in the table, except in a dropdown menu\n",
    "5. **Desired class for counterfactual(s):** specifies the class to generate counterfactuals for\n",
    "6. **Create what-if counterfactual:** displays side bar for counterfactual creation\n",
    "7. **Top ranked features plot:** displays in order the features to perturb to create counterfactuals of the desired class"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c0ae223",
   "metadata": {},
   "source": [
    "Clicking on \"Create what-if counterfactual\" results in a large side panel popping up."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79ea5f0b",
   "metadata": {},
   "source": [
    "![counterfactuals](img/tour-22.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ded0287",
   "metadata": {},
   "source": [
    "1. **Search features:** finds features to observe and change values\n",
    "2. **Sort counterfactual by ranked features:** sorts counterfactual examples in order of perturbation effect (see above for top ranked features plot)\n",
    "3. **Counterfactual Ex:** lists feature values of example counterfactuals in a given class\n",
    "4. **Predicted class:** lists the model prediction of a counterfactual's class given those changed features\n",
    "5. **Create your own counterfactual:** allows you to perturb your own features to modify the counterfactual\n",
    "6. **What-if counterfactual name:** allows you to name the counterfactual uniquely\n",
    "7. **Save as new datapoint:** saves the counterfactual you have created"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f4785ec",
   "metadata": {},
   "source": [
    "## Causal Inferencing"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a36ef394",
   "metadata": {},
   "source": [
    "![causal-1](img/tour-23.png)\n",
    "![causal-2](img/tour-24.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfd006cd",
   "metadata": {},
   "source": [
    "1. **Aggregate causal effects:** current tab, shows causality on average in this sample\n",
    "2. **Individual causal what-if:** shows causality for individual datapoints\n",
    "3. **Treatment policy:** shows treatment(s) to apply to achieve a particular outcome\n",
    "4. **Direct aggregate causal effect table:** displays the causal effect of each feature aggregated on the entire dataset and associated confidence statistics\n",
    "5. **Direct aggregate causal effect scatter plot:** visualizes the causal effects and confidence intervals of the points in the table"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ff6b14f",
   "metadata": {},
   "source": [
    "To get a granular view of causation on a particular datapoint, switch to the \"Individual causal what-if\" tab."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d926610",
   "metadata": {},
   "source": [
    "![causal-3](img/tour-25.png)\n",
    "![causal-4](img/tour-26.png)\n",
    "![causal-5](img/tour-27.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "178bf276",
   "metadata": {},
   "source": [
    "1. **X axis:** selects feature to plot on the x-axis\n",
    "2. **Y axis:** selects feature to plot on the y-axis\n",
    "3. **Individual causal scatter plot:** visualizes points in table as scatter plot\n",
    "4. **Set new treatment value (numerical):** shows slider to change the value of the numerical feature as a real-world intervention\n",
    "5. **Set new treatment value (categorical):** shows dropdown to select the value of the categorical feature"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e32e39b",
   "metadata": {},
   "source": [
    "Clicking on the \"Treatment policy\" tab switches to a view to help determine real-world interventions."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2af4659e",
   "metadata": {},
   "source": [
    "![causal-treatment1](img/tour-28.png)\n",
    "![causal-treatment2](img/tour-29.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5bc8c40c",
   "metadata": {},
   "source": [
    "1. **Set treatment feature:** selects feature to change as a real-world intervention\n",
    "2. **Recommended global treatment policy:** displays recommended interventions for data cohorts to improve target feature value\n",
    "3. **Average gains of alternative policies over no ____ treatment:** plots the target feature value with bars for treatment policy and no treatment policy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25e9f036",
   "metadata": {},
   "source": [
    "Scrolling down further shows treatment policies for individuals."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd6421dd",
   "metadata": {},
   "source": [
    "![causal-treatment3](img/tour-30.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "638131ff",
   "metadata": {},
   "source": [
    "1. **Show top datapoint samples ordered by causal effects for recommended treatment feature:** selects the number of datapoints to show in below table\n",
    "2. **Recommended individual treatment policy table:** lists in descending order the datapoints whose target features would be most improved by an intervention"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
